package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModel;
import hk.m4s.lr.repair.test.model.MaintenanceModle;
import hk.m4s.lr.repair.test.model.projectModle;
import hk.m4s.lr.repair.test.views.InnerGridView;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class DeviceMainListAdapter extends BaseAdapter {
    List<MaintenanceInfoModel> list = new ArrayList<>();
    List<projectModle> items;
    Context context;
    LayoutInflater inflater;
    PartslAdapter adapter;
    public DeviceMainListAdapter(Context context, List<MaintenanceInfoModel> data) {
        this.context = context;
        this.list = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_device_main, parent, false);
            holder.device_name = (TextView) convertView.findViewById(R.id.device_name);
            holder.device_num = (TextView) convertView.findViewById(R.id.device_num);
            holder.up = (ImageView) convertView.findViewById(R.id.up);
            holder.by_looks = (LinearLayout) convertView.findViewById(R.id.show_drvice);
            holder.nby_types = (RelativeLayout) convertView.findViewById(R.id.click_show);
            holder.select_device = (InnerGridView) convertView.findViewById(R.id.select_device);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nby_types.setTag(position);
        MaintenanceInfoModel businessModel = list.get(position);
        holder.device_name.setText(businessModel.getTitle());
        if(businessModel.isShowViews()){
            holder.up.setImageResource(R.mipmap.down);
            holder.by_looks.setVisibility(View.VISIBLE);
        }else{
            holder.up.setImageResource(R.mipmap.up);
            holder.by_looks.setVisibility(View.GONE);
        }
        try {
            if(businessModel.getMaintenanceProject()!=null) {
                items=new ArrayList<>();
                JSONArray obj = new JSONArray(businessModel.getMaintenanceProject());
                holder.device_num.setText(obj.length()+"");
                for (int i = 0; i < obj.length(); i++) {
                    projectModle modle = new projectModle();
                    modle.setName(obj.getJSONObject(i).get("name").toString());
                    modle.setState("1");
                    items.add(modle);
                }
                adapter = new PartslAdapter(context, items);
                holder.select_device.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.nby_types.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               int num=(Integer)v.getTag();
                MaintenanceInfoModel businessModel = list.get(num);
                if(businessModel.isShowViews()){
                   businessModel.setShowViews(false);
                }else{
                    businessModel.setShowViews(true);
                }
              notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView device_name;
        TextView device_num;
        ImageView up;
        LinearLayout by_looks;
        RelativeLayout nby_types;
        InnerGridView select_device;
    }
}
